package com.handpay.zztong.hp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.discovery.DiscoveryModleBean;
import java.util.ArrayList;

/**
 * Created by fplei on 2017/5/15.
 */

public class DiscoveryDataAdapter extends RecyclerView.Adapter<DiscoveryDataAdapter.ItemViewHolder>{
    private ArrayList<DiscoveryModleBean> modleBeens=new ArrayList<>();
    private static final int TYPE_ITEM = 0;
    private RecyclerViewClickListener clickListener;
    public DiscoveryDataAdapter(RecyclerViewClickListener mclickListener){
        this.clickListener=mclickListener;
    }
    public void setModleBeens(ArrayList<DiscoveryModleBean> datas){
        this.modleBeens=datas;
        notifyDataSetChanged();
    }
    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder;
        View view = null;
        if (viewType == TYPE_ITEM) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_discovery_item, null);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
        }
        itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public int getItemCount() {

        return modleBeens.size();
    }
    @Override
    public int getItemViewType(int position) {

        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {
        DiscoveryModleBean bean=modleBeens.get(position);
        HPLog.i(HPLog.TAG_RESULT,bean.toString());
        HPLog.i(HPLog.TAG_RESULT,"-position:"+position);
        if(bean!=null){
            holder.img_icon.setBackgroundResource(bean.getItem_icon());
            holder.itemTitle.setText(bean.getItem_title());
            if(bean.getIsNewFlag()>0){
                holder.img_newflag.setVisibility(View.VISIBLE);
            }else {
                holder.img_newflag.setVisibility(View.INVISIBLE);
            }

            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.OnItemClick(modleBeens.get(position));
                }
            });
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout ll_item;
        ImageView img_icon,img_newflag;
        TextView itemTitle;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ll_item = (RelativeLayout) itemView.findViewById(R.id.item_rela_root);
            img_newflag = (ImageView) itemView.findViewById(R.id.img_newflag);
            img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
            itemTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    public interface RecyclerViewClickListener{
        void OnItemClick(DiscoveryModleBean bean);
    }

    public void destory(){
        clickListener=null;
        modleBeens.clear();
        modleBeens=null;
    }
}
